package MWClient;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * <pre>
 * @Package : MWClient
 * @File    : ClientSender.java
 * <pre>
 *
 * @Author  : 차재우
 * @Date    : 2013. 7. 22.
 * @Version :
 */
class ClientSender implements Runnable {
	private Socket sock = null;
	private DataOutputStream out = null;
	private PrintWriter writer = null;
	private SerialQueue serialRecvQueue = null;

	/**
	 * <pre>
	 * @Comment : 생성자
	 * <pre>
	 *
	 * @param socket
	 */
	public ClientSender(Socket socket) {
		this.sock = socket;
		this.serialRecvQueue = MWClient.recvQueue;

		try {
			out = new DataOutputStream(sock.getOutputStream());
			writer = new PrintWriter(out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * <pre>
	 * @Comment : 클라이언트 전송 스레드 시작 루틴
	 * <pre>
	 *
	 * @Method  : run
	 */
	public void run() {
		while (out != null) {
			try {
				if (this.serialRecvQueue.IsEmpty() == true)
					continue;

				MobileData mobileData = serialRecvQueue.Pop();
				String strBuf = mobileData.getStrCardData();

				writer.println(strBuf);
				writer.flush();

				System.out.println("\n***************************************************************************************************************************************");
				System.out.println("Mobile -------------------> Middleware");
				System.out.println(strBuf);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
